package com.zhuo.schoolproject.controller;

import cn.dev33.satoken.util.SaResult;
import com.zhuo.schoolproject.utils.FilesUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
public class TestController {
    private final FilesUtils filesUtils;

    @PostMapping("/file")
    public SaResult testFile(@RequestParam("file")MultipartFile file){
        Map<String,Object> upload = filesUtils.upload(file);
        return SaResult.ok(upload.get("fileName").toString());
    }

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping("/redis")
    public String testRedis() {
        try {
            redisTemplate.opsForValue().set("test-key", "test-value");
            Object value = redisTemplate.opsForValue().get("test-key");
            return "Redis连接成功: " + value;
        } catch (Exception e) {
            return "Redis连接失败: " + e.getMessage();
        }
    }
}